package com.cctv.video.base.adapter;

import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.app.Context;

import java.util.HashMap;


public class ItemViewHolder {
    private HashMap<Integer, Component> mViews;
    private int mPosition;
    private Component mConvertView;
    private Context mContext;
    private int mLayoutId;

    public ItemViewHolder(Context context, ComponentContainer parent, int layoutId, int position) {
        mContext = context;
        mLayoutId = layoutId;
        this.mPosition = position;
        this.mViews = new HashMap<>();
        mConvertView = LayoutScatter.getInstance(mContext).parse(layoutId, null, false);

        mConvertView.setTag(this);
    }

    public static ItemViewHolder get(Context context, Component convertView,
                                     ComponentContainer parent, int layoutId, int position) {
        if (convertView == null) {
            return new ItemViewHolder(context, parent, layoutId, position);
        } else {
            ItemViewHolder holder = (ItemViewHolder) convertView.getTag();
            holder.mPosition = position;
            return holder;
        }
    }

    public int getPosition() {
        return mPosition;
    }

    public int getLayoutId() {
        return mLayoutId;
    }

    /**
     * 通过viewId获取控件
     *
     * @param viewId
     * @return
     */
    public <T extends Component> T getView(int viewId) {
        Component view = mViews.get(viewId);
        if (view == null) {
              view = mConvertView.findComponentById(viewId);
            mViews.put(viewId, view);
        }
        return (T) view;
    }


    public Component getConvertView() {
        return mConvertView;
    }

    /**
     * 设置TextView的�??
     *
     * @param viewId
     * @param text
     * @return
     */
    public ItemViewHolder setText(int viewId, String text) {
        Text tv = getView(viewId);
        tv.setText(text);
        return this;
    }

   /* public ViewHolder setImageResource(int viewId, int resId) {
        Image view = getView(viewId);
        view.setImageResource(resId);
        return this;
    }

    public ViewHolder setImageBitmap(int viewId, Bitmap bitmap) {
        ImageView view = getView(viewId);
        view.setImageBitmap(bitmap);
        return this;
    }

    public ViewHolder setImageDrawable(int viewId, Drawable drawable) {
        ImageView view = getView(viewId);
        view.setImageDrawable(drawable);
        return this;
    }*/

/*    public ViewHolder setBackgroundColor(int viewId, int color) {
        Component view = getView(viewId);
        view.setBackground(color);
        return this;
    }
    public ViewHolder setTextColorRgb(int viewId, int color) {
        ohos.global.resource.ResourceManager resManager = mContext.getResourceManager();
        Color mColor = resManager.getElement(color);

        Text view = getView(viewId);
        view.setTextColor(ResourceTable.Color_red);
        return this;
    }*/

/*
    public ViewHolder setTextColorRgb(int viewId, int r,int g,int b) {
        ShapeElement element = new ShapeElement();
        element.setRgbColor(new RgbColor(r, g, b));
        int dddd = ResourceTable.Color_red;
        ohos.global.resource.ResourceManager resManager = mContext.getResourceManager();
        int color = resManager.getElement(ResourceTable.Color_red).getColor();

        Text view = getView(viewId);
        view.setTextColor(element);
        return this;
    }*/



/*
    public ViewHolder setVisible(int viewId, boolean visible) {
        View view = getView(viewId);
        view.setVisibility(visible ? View.VISIBLE : View.GONE);
        return this;
    }

    public ViewHolder linkify(int viewId) {
        TextView view = getView(viewId);
        Linkify.addLinks(view, Linkify.ALL);
        return this;
    }*/

  /*  public ViewHolder setTypeface(Typeface typeface, int... viewIds) {
        for (int viewId : viewIds) {
            TextView view = getView(viewId);
            view.setTypeface(typeface);
            view.setPaintFlags(view.getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG);
        }
        return this;
    }
*/
   /* public ViewHolder setProgress(int viewId, int progress) {
        ProgressBar view = getView(viewId);
        view.setProgress(progress);
        return this;
    }

    public ViewHolder setProgress(int viewId, int progress, int max) {
        ProgressBar view = getView(viewId);
        view.setMax(max);
        view.setProgress(progress);
        return this;
    }

    public ViewHolder setMax(int viewId, int max) {
        ProgressBar view = getView(viewId);
        view.setMax(max);
        return this;
    }

    public ViewHolder setRating(int viewId, float rating) {
        RatingBar view = getView(viewId);
        view.setRating(rating);
        return this;
    }

    public ViewHolder setRating(int viewId, float rating, int max) {
        RatingBar view = getView(viewId);
        view.setMax(max);
        view.setRating(rating);
        return this;
    }

    public ViewHolder setTag(int viewId, Object tag) {
        Component view = getView(viewId);
        view.setTag(tag);
        return this;
    }

    public ViewHolder setTag(int viewId, int key, Object tag) {
        Component view = getView(viewId);
        view.setTag(key, tag);
        return this;
    }

    public ViewHolder setChecked(int viewId, boolean checked) {
        Checkable view = (Checkable) getView(viewId);
        view.setChecked(checked);
        return this;
    }*/

    /**
     * 关于事件
     */
    public ItemViewHolder setClickedListener(int viewId,
                                             Component.ClickedListener listener) {
        Component view = getView(viewId);
        view.setClickedListener(listener);
        return this;
    }

    public ItemViewHolder setTouchEventListener(int viewId,
                                                Component.TouchEventListener listener) {
        Component view = getView(viewId);
        view.setTouchEventListener(listener);
        return this;
    }

    public ItemViewHolder setLongClickedListener(int viewId,
                                                 Component.LongClickedListener listener) {
        Component view = getView(viewId);
        view.setLongClickedListener(listener);
        return this;
    }

    public ItemViewHolder setDoubleClickedListener(int viewId,
                                                   Component.DoubleClickedListener listener) {
        Component view = getView(viewId);
        view.setDoubleClickedListener(listener);
        return this;
    }

}
